package com.panshi.controller;

import com.panshi.api.bean.ArticleBean;
import com.panshi.pojo.ArticleNoVO;
import com.panshi.api.util.PageSizeVO;
import com.panshi.servers.ArticleNoServers;
import com.panshi.utils.UserMessagePort;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

/**
 * @author XiaoWangYa
 * @version 1.0
 * @date 2020/8/4 0004 下午 8:24
 */
@Slf4j
@Controller
@ApiModel("文章审核Controller层")
public class ArticleNoControlle {

    @Autowired  // 文章未审核
    private ArticleNoServers yb_articleNoServers;

    @Autowired  // 文章
    private ArticleController articleController;

    @Autowired  // 文章审核发送邮箱提醒用户
    private UserMessagePort userMessagePort;

    @PostMapping("/addArticNoInfo")
    @ResponseBody
    @ApiModelProperty("新增文章未审核信息")
    public String addArticNoInfo (@RequestBody ArticleNoVO yb_ArticleNo_VO) {
        log.info("新增文章未审核信息 --> " + yb_ArticleNo_VO);
        // 删除文章表指定文章
        articleController.delArticleInfo(yb_ArticleNo_VO.getAid());
        // 新增未审核
        yb_articleNoServers.addArticleNoInfo(yb_ArticleNo_VO);
        return "ok";
    }

    @PostMapping("/deleteArticleNoInfo/{id}")
    @ResponseBody
    @ApiModelProperty("伪删除文章未审核信息")
    public String deleteArticleNoInfo (@PathVariable int id) {
        log.info("伪删除文章未审核信息 --> " + id);
        yb_articleNoServers.deleteArticleNoInfo(id);
        return "ok";
    }

    @PostMapping("/delArticleNoInfo/{id}")
    @ResponseBody
    @ApiModelProperty("删除文章未审核信息")
    public String delArticleNoInfo (@PathVariable int id) {
        log.info("删除文章未审核信息 --> " + id);
        yb_articleNoServers.delArticleNoInfo(id);
        return "ok";
    }

    @PostMapping("/queryAssignArticleNoInfo")
    @ResponseBody
    @ApiModelProperty("查询指定用户文章未审核信息")
    public PageSizeVO queryAssignUserArticleNoInfo (@RequestBody PageSizeVO yb_PageSize_VO) {
        log.info("查询指定用户文章未审核信息 --> " + yb_PageSize_VO);
        // 设置分页初始页
        yb_PageSize_VO.setStartIndex(yb_PageSize_VO.getStartIndexInfo());
        // 获取未审核数据
        List<ArticleNoVO> articleNoVOS = yb_articleNoServers.queryAssignUserArticleNoInfo(yb_PageSize_VO);
        // 返回数据
        PageSizeVO page = new PageSizeVO();
        page.setData(articleNoVOS); // 未审核数据
        page.setCount(yb_articleNoServers.queryArticleNoCount());   // 获取总数据长度
        return page;
    }

    @PostMapping("/queryArticleNoAll")
    @ResponseBody
    @ApiModelProperty("查询所有未审核文章")
    public PageSizeVO queryArticleNoAll(@RequestBody PageSizeVO yb_PageSize_VO) {
        log.info("查询所有未审核文章 --> " + yb_PageSize_VO);
        // 设置分页初始页
        yb_PageSize_VO.setStartIndex(yb_PageSize_VO.getStartIndexInfo());
        List<ArticleNoVO> articleNoVOS = yb_articleNoServers.queryArticleNoAll(yb_PageSize_VO);
        // 返回数据
        PageSizeVO page = new PageSizeVO();
        page.setData(articleNoVOS); // 未审核数据
        page.setCount(yb_articleNoServers.queryArticleNoCount());   // 获取总数据长度
        return page;
    }

    @PostMapping("/updateAssignArticleNoInfo")
    @ResponseBody
    @ApiModelProperty("修改指定用户文章未审核信息")
    public String updateAssignArticleNoInfo (@RequestBody ArticleNoVO yb_ArticleNo_VO) throws Exception {
        log.info("修改指定用户文章未审核信息 --> " + yb_ArticleNo_VO);
        // 查询指定文章未审核信息
        ArticleNoVO articleNoVO = yb_articleNoServers.queryAssignArticleNoInfo(yb_ArticleNo_VO.getAid());
        System.out.println(articleNoVO);
        // 文章新增所需数据赋值
        ArticleBean articleBean = new ArticleBean();
        articleBean.setUid(articleNoVO.getUid());   // 用户Id
        articleBean.setTid(yb_ArticleNo_VO.getTid());   // 文章类型Id
        articleBean.setA_title(articleNoVO.getA_title());  // 文章标题
        articleBean.setReasons(yb_ArticleNo_VO.getReasons());  // 文章简介
        articleBean.setA_content(articleNoVO.getA_content()); // 文章内容
        articleBean.setState(yb_ArticleNo_VO.getState()); // 文章状态
        System.out.println(articleBean);
        if (yb_ArticleNo_VO.getState() != null && yb_ArticleNo_VO.getState().equals("2")) {   // 审核通过
            // 新增文章
            articleController.addArticleInfo(articleBean);
            // 站内消息表新增数据
            articleBean.setState("0");  // 站内文章状态     上架
            articleController.addInstation(articleBean);

            // 文章审核通过发送邮箱提醒用户
            userMessagePort.setemailarticleno(articleNoVO.getUid(), Integer.valueOf(yb_ArticleNo_VO.getState()), yb_ArticleNo_VO.getA_title());

            // 删除审核未通过信息
            yb_articleNoServers.delArticleNoInfo(articleNoVO.getAid());
        } else if (yb_ArticleNo_VO.getState() != null && yb_ArticleNo_VO.getState().equals("0")) {    // 未审核文章删除
            articleBean.setState("1");  // 站内文章状态     下架
            // 站内消息表新增数据
            articleController.addInstation(articleBean);
        } else if (yb_ArticleNo_VO.getState() != null && yb_ArticleNo_VO.getState().equals("3")) {    // 审核未通过
            articleBean.setState("2");  // 站内文章状态     删除
            // 站内消息表新增数据
            articleController.addInstation(articleBean);

            // 文章审核未通过发送邮箱提醒用户
            userMessagePort.setemailarticleno(articleNoVO.getUid(), Integer.valueOf(yb_ArticleNo_VO.getState()), yb_ArticleNo_VO.getA_title());
        }
        // 修改未审核文章信息
        yb_articleNoServers.updateAssignArticleNoInfo(yb_ArticleNo_VO);
        return "ok";
    }

}
